package com.rjpacket.chileme.app;

import android.app.ActionBar;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.*;
import com.rjpacket.chileme.app.adapters.CanteenListAdapter;
import com.rjpacket.chileme.app.models.Restaurant;
import com.rjpacket.chileme.app.tasks.ListTaskInterface;
import com.rjpacket.chileme.app.tasks.StoreListTask;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.LinkedList;



public class CanteenActivity extends ActionBarActivity implements AdapterView.OnItemClickListener, ListTaskInterface, AdapterView.OnItemSelectedListener {

    private Spinner mSpinnerTaste;//全部口味
    private Spinner mSpinnerOrder;//默认排序
    private Spinner mSpinnerDiscount;//优惠活动

    private LayoutInflater mInflater;
    private ListView mStores;
    private LinkedList<Restaurant> data;
    private CanteenListAdapter adapter;
    private ArrayList<String> mOrder;
    private ArrayAdapter<String> mOrderAdapter;
    private ArrayList<String> mTaste;
    private ArrayAdapter<String> mTasteAdapter;
    private String url;
    private RelativeLayout relativeLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_canteen);

        relativeLayout = (RelativeLayout)findViewById(R.id.activity_canteen_error_bg);

        ActionBar mActionBar = getActionBar();

        if (mActionBar != null) {
            //添加后退按钮
            mActionBar.setDisplayHomeAsUpEnabled(true);
            //
            mActionBar.setDisplayShowCustomEnabled(true);
            //去掉图标
            mActionBar.setIcon(new ColorDrawable(Color.TRANSPARENT));

            //加载自定义标题
            mInflater = LayoutInflater.from(this);
            View view = mInflater.inflate(R.layout.actionbar_canteen_title, null);
            mActionBar.setCustomView(view);

            mActionBar.setDisplayShowTitleEnabled(false);
        }

        //////////////////////////////////////////////////////
        //三个Spinner

        mSpinnerDiscount = (Spinner) findViewById(R.id.canteen_spinner_discount);
        mSpinnerOrder = (Spinner) findViewById(R.id.canteen_spinner_order);
        mSpinnerTaste = (Spinner) findViewById(R.id.canteen_spinner_taste);

        //全部口味
        mTaste = new ArrayList<String>();
        mTaste.add("中式");
        mTaste.add("西式");
        mTaste.add("日式");
        mTaste.add("韩式");
        mTaste.add("烧烤");
        mTaste.add("奶茶");
        mTaste.add("汉堡");
        mTaste.add("清真");
        mTasteAdapter = new ArrayAdapter<String>(
                this,
                R.layout.mz_item_canteen_txt,
                R.id.canteen_spinner_title,
                mTaste
        );
        mSpinnerTaste.setAdapter(mTasteAdapter);
        mSpinnerTaste.setOnItemSelectedListener(this);

        //排序
        mOrder = new ArrayList<String>();
        mOrder.add("默认排序");
        mOrder.add("销量最大");
        mOrder.add("速度最快");
        mOrder.add("起送价最低");
        mOrderAdapter = new ArrayAdapter<String>(
                this,
                R.layout.mz_item_canteen_txt,
                R.id.canteen_spinner_title,
                mOrder
        );
        mSpinnerOrder.setAdapter(mOrderAdapter);

        mSpinnerOrder.setOnItemSelectedListener(this);

        /////////////////////////////////////////////////////

        //ListView
        //外卖餐厅列表
        mStores = (ListView) findViewById(R.id.canteen_listview_stores);
        if (mStores != null) {
            data = new LinkedList<Restaurant>();
            //绑定适配器
            adapter = new CanteenListAdapter(data, this);
            mStores.setAdapter(adapter);
            url = Constants.CANTEEN_LIST_URL;
            checkNetWork();
            StoreListTask task = new StoreListTask(this);
            task.execute(url);
            //绑定点击事件
            mStores.setOnItemClickListener(this);
        }
    }

    /**
     * 检测网络状态
     */
    private void checkNetWork() {

        ConnectivityManager manager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);

        NetworkInfo info = manager.getActiveNetworkInfo();

        if (info != null) {
            boolean b = info.isAvailable();
            if (!b) {
                Toast.makeText(this, "您的网络不可用，请检查网络后再试", Toast.LENGTH_SHORT).show();
            }
        }
    }

    /**
     * 点击进入店铺详情
     *
     * @param parent
     * @param view
     * @param position
     * @param id
     */
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }

    /**
     * 获得回调的JSON数据
     *
     * @param result
     */
    @Override
    public void processListResult(JSONObject result) {
        if (result != null) {
            Restaurant res = new Restaurant();
            res.parseJSON(result);

            data.addAll(0, res.getData());
            adapter.notifyDataSetChanged();
        }
    }

    /**
     * Spinner的选中事件
     * @param parent
     * @param view
     * @param position
     * @param id
     */
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        int viewId = view.getId();
        switch (viewId){
            //如果点击的是口味
            case R.id.canteen_spinner_taste:
                LinkedList<Restaurant> newRes = new LinkedList<Restaurant>();
                String flavour = mTaste.get(position);
                //如果点击的是全部口味,则再执行
                if (position==0){
                    StoreListTask task = new StoreListTask(this);
                    task.execute(url);
                }
                for (Restaurant restaurant : data) {
                    //如果商店的口味中包含当前被选择的口味,则添加到新的集合中
                    if (restaurant.getFlavors().contains(flavour)){
                        mStores.setVisibility(View.VISIBLE);
                        relativeLayout.setVisibility(View.GONE);
                        newRes.add(0,restaurant);
                    }else{
                        //TODO 没有这个口味,则显示没有找到筛选条件的餐厅
                        relativeLayout.setVisibility(View.VISIBLE);
                        mStores.setVisibility(View.GONE);
                    }
                }
                data.clear();
                data.addAll(0,newRes);
                adapter.notifyDataSetChanged();
                break;
            case R.id.canteen_spinner_order:

                break;
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
}
